﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using NUnit.Framework;
using COFE.Core.Lister;
using COFE.Core.Logger;

namespace COFE.Core.Profile
{
    public class VolumeManager : IVolumeManager
    {
        #region Constructor

        public VolumeManager(IUserProfile userProfile)
        {
            UserProfile = userProfile;            
        }

        #endregion

        #region Methods

        void registerVolume(IVolume volume)
        {
            string listerKey = volume.ListerKey;
            if (volume.IsPrimaryDirectoryLister())
            {
                IPrimaryDirectoryLister primaryLister = (IPrimaryDirectoryLister)volume.DirectoryLister;
                lock (_primaryVolumes)
                {
                    if (!PrimaryVolumes.Contains(volume))
                        _primaryVolumes.Add(volume);

                    if (primaryLister.RootTempPath != null)
                        TempManager = new DefaultTempManager(primaryLister.RootTempPath, UserProfile.UserName);

                    foreach (var rootDirectory in primaryLister.RootDirectories)
                        _rootDirectories.Add(rootDirectory);
                }
            }

            if (volume.IsSecondaryDirectoryLister())
            {
                lock (_secondaryVolumes)
                {
                    if (!SecondaryVolumes.Contains(volume))
                        _secondaryVolumes.Add(volume);
                }
            }
        }

        public virtual void RegisterVolume(string listerKey)
        {
            Assert.IsNotNull(UserProfile);
            IVolume volume = COFEShared.VolumeRegistrar.LookupRegisterVolume(listerKey);
            registerVolume(volume);
        }


        #endregion

        #region Data

        List<IVolume> _primaryVolumes = new List<IVolume>();
        List<IVolume> _secondaryVolumes = new List<IVolume>();
        List<IDirectoryInfo> _rootDirectories = new List<IDirectoryInfo>();
        ITempManager _tempManager = new NullTempManager();

        #endregion

        #region Public Properties

        public IVolume this[string listerKey, bool lookupPrimary = false, bool lookupSecondary = false]
        {
            get
            {
                if (lookupPrimary)
                    foreach (var vol in PrimaryVolumes) if (vol.ListerKey == listerKey) return vol;
                if (lookupSecondary)
                    foreach (var vol in COFEShared.VolumeRegistrar.GetSecondaryVolumes()) if (vol.ListerKey == listerKey) return vol;
                return null;
            }
        }

        public IUserProfile UserProfile { get; private set; }
        public ITempManager TempManager { get { return _tempManager; } private set { _tempManager = value; } }
        public virtual IDirectoryInfo[] RootDirectories { get { return _rootDirectories.ToArray(); } }
        public virtual List<IVolume> PrimaryVolumes { get { return _primaryVolumes; } }
        public virtual List<IVolume> SecondaryVolumes { get { return _secondaryVolumes; } }

        #endregion




    }
}
